//
//  YFNetWorkUtil.m
//  PSB
//
//  Created by 孙云飞 on 2017/9/13.
//  Copyright © 2017年 孙云飞. All rights reserved.
//

#import "YFNetWorkUtil.h"
#import <AFNetworking/AFNetworking.h>
#define BAD_NETWORK @"服务器连接失败"
static AFHTTPSessionManager *manager;//单例创建
@implementation YFNetWorkUtil
+ (instancetype)shareNetWorkUtil{
    
    static YFNetWorkUtil *util = nil;
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        util = [[self alloc]init];
    });
    return util;
}

//单例创建afn，因为有内存泄漏
-(AFHTTPSessionManager *)sharedHTTPSession{
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        manager = [AFHTTPSessionManager manager];
        // 设置超时时间
        [manager.requestSerializer willChangeValueForKey:@"timeoutInterval"];
        manager.requestSerializer.timeoutInterval = 8.f;
        [manager.requestSerializer didChangeValueForKey:@"timeoutInterval"];
    });
    return manager;
}

//根请求类
-(void)AFNsetting:(NSString *)httpStr param:(NSDictionary *)param success:(void(^)(id respond))successBlock failure:(void(^)(id error))failureBlock
{
    AFHTTPSessionManager *manager = [self sharedHTTPSession];
    manager.responseSerializer.acceptableContentTypes = [NSSet setWithObjects:@"application/json", @"text/html", @"text/json", @"text/javascript",@"text/plain", nil];
    [manager POST:httpStr parameters:param progress:^(NSProgress * _Nonnull uploadProgress) {
        
    } success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
        
        //防止返回data
        if ([responseObject isKindOfClass:[NSData class]]) {
            
            responseObject = [NSJSONSerialization JSONObjectWithData:responseObject options:NSJSONReadingMutableLeaves error:nil];
        }
        if([responseObject[@"status"] intValue] == 1){
            
            successBlock(responseObject[@"data"]);
        }else
            failureBlock(BAD_NETWORK);
        
    } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
        
        failureBlock(BAD_NETWORK);
    }];
    
}


//根请求类
-(void)afnGetsetting:(NSString *)httpStr success:(void(^)(id respond))successBlock failure:(void(^)(id error))failureBlock
{
    AFHTTPSessionManager *manager = [self sharedHTTPSession];
    manager.responseSerializer.acceptableContentTypes = [NSSet setWithObjects:@"application/json", @"text/html", @"text/json", @"text/javascript",@"text/plain", nil];
    [manager GET:httpStr parameters:nil progress:^(NSProgress * _Nonnull downloadProgress) {
        
    } success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
        
        //防止返回data
        if ([responseObject isKindOfClass:[NSData class]]) {
            
            responseObject = [NSJSONSerialization JSONObjectWithData:responseObject options:NSJSONReadingMutableLeaves error:nil];
        }
        
        if([responseObject[@"status"] intValue] == 1){
            
            successBlock(responseObject[@"data"]);
        }else
            failureBlock(BAD_NETWORK);
    } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
        failureBlock(BAD_NETWORK);
    }];
    
}

//获取服务器产品版本
- (void)obtainVersion:(void(^)(id data))successBlock failure:(void(^)(id error))failureBlock{
    
    NSString *url = @"https://app.koudaisoushu.com/system/version.json";
    [self afnGetsetting:url success:^(id respond) {
        
        successBlock(respond);
    } failure:^(id error) {
        
        failureBlock(error);
    }];
}

@end

